package org.xserver.common.util;

import java.util.Random;

import org.xserver.component.exception.InvalidArgumentError;

import static org.xserver.common.util.CharsetUtil.*;

public class RandomUtil {
	public static final Random random = new Random();

	public static String random(int length) {
		return random(length, true, true);
	}

	public static String random(int length, boolean character, boolean number) {
		StringBuffer sb = new StringBuffer();

		String[] randomPool;
		if (character && number) {
			randomPool = CHARACTER_NUMBER;
		} else if (character) {
			randomPool = CHARACTER;
		} else if (number) {
			randomPool = NUMBER;
		} else {
			throw new InvalidArgumentError("character or number",
					"either character or number must be true");
		}

		for (int i = 0; i < length; i++) {
			int index = random.nextInt(randomPool.length);
			sb.append(randomPool[index]);
		}

		return sb.toString();
	}
}
